﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;

using AtletIS.Shared.Data.aDbDataSetTableAdapters;
using AtletIS.Shared.Helpers;
using AtletIS.Shared.Entities;
using System.Windows.Interop;

namespace AtletIS.Shared.WpfControls
{
    public partial class PressControl : UserControl
    {
        private CompetitionsTableAdapter taCompetitions = new CompetitionsTableAdapter();
        private DisciplineListEntity discipline;
        private UIElement detail;
        private string detailCode = "SL";
        
        // Constructor
        public PressControl()
        {
            InitializeComponent();
        }

        // Loaded
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            Globals.ActiveCompetition = CompetitionHelper.ConvertToEntity(DatabaseHelper.GetCompetitionsActive().Rows[0]); // this.taCompetitions.GetActive().Rows[0]);

            this.cbTitle.title1 = Globals.ActiveCompetition.Name;
            this.cbTitle.title2 = Globals.ActiveCompetition.DateFrom.ToShortDateString() + " - " + Globals.ActiveCompetition.City;

            DisciplineListHelper.FillListBoxWPF(lbDisciplineList);

            if (this.lbDisciplineList.Items.Count > 0)
            {
                this.discipline = ((DisciplineListBox1)lbDisciplineList.Items[0]).discipline;
                this.RefreshDetail();
            }
        }

        // Discipline changed
        private void lbDisciplineList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.discipline = null;

            try
            {
                if (typeof(DisciplineListBox1) == lbDisciplineList.SelectedItem.GetType())
                    this.discipline = ((DisciplineListBox1)lbDisciplineList.SelectedItem).discipline;

                if (typeof(DisciplineListBox2) == lbDisciplineList.SelectedItem.GetType())
                    this.discipline = ((DisciplineListBox2)lbDisciplineList.SelectedItem).discipline;

                if (typeof(DisciplineListBox3) == lbDisciplineList.SelectedItem.GetType())
                    this.discipline = ((DisciplineListBox3)lbDisciplineList.SelectedItem).discipline;

                this.RefreshDetail();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Nastala chyba!" + Environment.NewLine + ex.Message, "Press", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        // Refresh Detail View
        private void RefreshDetail()
        {
            if (this.discipline != null)
            {
                if (detail != null && this.gMain.Children.Contains(detail))
                    this.gMain.Children.Remove(detail);

                switch (detailCode)
                {
                    case "SL":
                        detail = new StartList(this.discipline);
                        break;

                    case "RES":
                        detail = new Results(this.discipline);
                        break;

                    case "CHIPRES":
                        detail = new ChipResults(this.discipline);
                        break;
                    
                    case "CHIP":
                        detail = new ChipViewer(this.discipline);
                        break;

                    case "LIVE":
                        detail = new LiveResults(this.discipline);
                        break;
                }

                this.gMain.Children.Add(detail);

                Grid.SetColumn(detail, 1);
                Grid.SetRow(detail, 1);
            }
        }

        #region Buttons

        // Start List
        private void btnStartList_Click(object sender, RoutedEventArgs e)
        {
            this.detailCode = "SL";
            this.RefreshDetail();
        }

        // Results
        private void btnResults_Click(object sender, RoutedEventArgs e)
        {
            this.detailCode = "RES";
            this.RefreshDetail();
        }

        // Live Result
        private void btnLiveResults_Click(object sender, RoutedEventArgs e)
        {
            this.detailCode = "LIVE";
            this.RefreshDetail();
        }

        // Chip Results
        private void btnChipResults_Click(object sender, RoutedEventArgs e)
        {
            this.detailCode = "CHIPRES";
            this.RefreshDetail();
        }

        // Chip Viewer
        private void btnChipViewer_Click(object sender, RoutedEventArgs e)
        {
            this.detailCode = "CHIP";
            this.RefreshDetail();
        }

        // Close
        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            HwndSource source = (HwndSource)PresentationSource.FromVisual(sender as Button);
            System.Windows.Forms.Control ctl = System.Windows.Forms.Control.FromChildHandle(source.Handle);
            ctl.FindForm().Close();

            System.Windows.Forms.Application.Exit();
        }

        #endregion

        

    }
}
